package com.linkhub.oms.admin.modules.stock.domain.dto;

import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.wms.domain.dto.ProcessOrderNoticeDTO;
import com.linkhub.oms.common.util.WmsUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ConfirmCombineProcessingDTO
 * @Description TODO
 * @Author XieYu
 * @Date 2024/7/7 22:31
 * @Version 1.0
 */
@Data
public class ConfirmCombineProcessingDTO implements Serializable {
    @ApiModelProperty(value = "用户id")
    private String userId;

    @ApiModelProperty(value = "仓库")
    private String stashName;

    @ApiModelProperty(value = "业务类型")
    private String businessType;

    private String businessNo;

    private List<Product> sourceProducts;

    private List<Product> targetProducts;

    @Data
    public static class Product {
        private String skuId;
        private Integer quantity;
        private String inventoryType;
    }

    public static ConfirmCombineProcessingDTO convert(ProcessOrderNoticeDTO processOrderNoticeDTO) {
        if (Objects.nonNull(processOrderNoticeDTO)) {
            ConfirmCombineProcessingDTO confirmCombineProcessingDTO = new ConfirmCombineProcessingDTO();
            confirmCombineProcessingDTO.setStashName(processOrderNoticeDTO.getWarehouseCode());
            confirmCombineProcessingDTO.setBusinessNo(processOrderNoticeDTO.getProcessOrderCode());
            List<Product> sourceProducts = Lists.newArrayListWithCapacity(processOrderNoticeDTO.getMaterialitems().size());
            for (ProcessOrderNoticeDTO.MaterialItem materialItem : processOrderNoticeDTO.getMaterialitems()) {
                Product source = new Product();
                confirmCombineProcessingDTO.setUserId(WmsUtil.getUserId(materialItem.getItemCode()));
                source.setSkuId(WmsUtil.getSkuId(materialItem.getItemCode()));
                source.setQuantity(materialItem.getQuantity());
                source.setInventoryType(materialItem.getInventoryType());
                sourceProducts.add(source);
            }
            confirmCombineProcessingDTO.setSourceProducts(sourceProducts);

            List<Product> targetProducts = Lists.newArrayListWithCapacity(processOrderNoticeDTO.getProductitems().size());
            for (ProcessOrderNoticeDTO.ProductItem productItem : processOrderNoticeDTO.getProductitems()) {
                Product target = new Product();
                target.setSkuId(WmsUtil.getSkuId(productItem.getItemCode()));
                target.setQuantity(productItem.getQuantity());
                target.setInventoryType(productItem.getInventoryType());
                targetProducts.add(target);
            }
            confirmCombineProcessingDTO.setTargetProducts(targetProducts);
            return confirmCombineProcessingDTO;
        }
        return null;
    }
}
